package com.lb.uts.web;

import com.lb.uts.constants.BaseResponse;
import com.lb.uts.constants.UTSConstants;
import com.lb.uts.entity.System;
import com.lb.uts.service.impl.SystemService;
import com.lb.uts.util.IdUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统操作
 * @Author liangb
 * @Date 2019/4/26 16:10
 */
@Controller
@RequestMapping("system")
public class SystemController extends UtsBaseController{

    private static final Logger logger = LoggerFactory.getLogger(SystemController.class);

    @Autowired
    private SystemService systemService;

    /*
    查询全部，不分页
     */
    @RequestMapping("list")
    public String list(Model model) {
        model.addAttribute("list", systemService.queryByEntity(new System()));
        return "system/list";
    }

    @RequestMapping("add")
    @ResponseBody
    @Validated
    public BaseResponse add(System system) {
        logger.info("插入system:" + system.toString());
        system.setId(IdUtils.nextUUID());
        system.setCreateTime(LocalDate.now());
        system.setUpdateTime(LocalDateTime.now());
        int num = systemService.insert(system);
        return num == 1 ? wrapSuccessResp(BaseResponse.class) : wrapErrResp(BaseResponse.class, "插入失败");
    }

    @RequestMapping("delete/{id}")
    @ResponseBody
    public BaseResponse delete(@PathVariable("id") String id) {
        logger.info("删除system:" + id);
        int num = systemService.deleteById(id);
        return num == 1 ? wrapSuccessResp(BaseResponse.class) : wrapErrResp(BaseResponse.class, "删除失败");
    }

    @RequestMapping("update")
    @ResponseBody
    @Validated
    public BaseResponse update(System system) {
        logger.info("更新system:" + system.toString());
        String id = system.getId();
        if (StringUtils.isBlank(id)) {
            return wrapErrResp(BaseResponse.class, "id不能为空");
        }
        System inDb = systemService.getById(id);
        if (inDb == null) {
            return wrapErrResp(BaseResponse.class, "记录不存在");
        }
        inDb.setUpdateTime(LocalDateTime.now());
        inDb.setSystemDesc(system.getSystemDesc());
        inDb.setSystemName(system.getSystemName());
        int num = systemService.update(inDb);
        return num == 1 ? wrapSuccessResp(BaseResponse.class) : wrapErrResp(BaseResponse.class, "更新失败");
    }

    @RequestMapping("edit/{id}")
    @ResponseBody
    public Map<String,String> editSystem(@PathVariable("id") String id){
        Map<String,String> result = new HashMap<>();
        System system = systemService.getById(id);
        if(system==null){
            result.put("returnCode","0001");
            result.put("returnMsg","记录不存在");
        }else {
            result.put("returnCode", UTSConstants.SUCCESS_CODE);
            result.put("id",system.getId());
            result.put("systemDesc",system.getSystemDesc());
            result.put("systemName",system.getSystemName());
        }
        return  result;
    }
}
